home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / util / flyConfig / flyConfigDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-20  |  12.5 KB  |  488 lines

  1. // flyConfigDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "flyConfig.h"
  6. #include "flyConfigDlg.h"
  7. #include <gl\gl.h>
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. CDC *cdc=0;
  16. HGLRC hglrc=0;
  17.  
  18. PIXELFORMATDESCRIPTOR rendermodes[256];
  19. DEVMODE    videomodes[256];
  20. int numrendermodes=0;
  21. int selrendermode=0;
  22. int numvideomodes=0;
  23. int selvideomode=0;
  24. int colorbits=0;
  25. int depthbits=0;
  26. int stencilbits=0;
  27. float brightness=1.0f;
  28.  
  29. void FlyGetProfile(LPTSTR section, LPTSTR key, LPBYTE lpData, DWORD cbData)
  30. {
  31.     HKEY key1,key2,key3,key4;
  32.  
  33.     if (ERROR_SUCCESS==RegOpenKeyEx(HKEY_CURRENT_USER,"Software",0,KEY_READ,&key1))
  34.     {
  35.         if (ERROR_SUCCESS==RegOpenKeyEx(key1,"Paralelo",0,KEY_READ,&key2))
  36.         {
  37.             if (ERROR_SUCCESS==RegOpenKeyEx(key2,"Fly3D",0,KEY_READ,&key3))
  38.             {
  39.                 if (ERROR_SUCCESS==RegOpenKeyEx(key3,section,0,KEY_READ,&key4))
  40.                 {
  41.                     RegQueryValueEx(key4, key, 0, 0, lpData, &cbData);
  42.                     RegCloseKey(key4);
  43.                 }
  44.                 RegCloseKey(key3);
  45.             }
  46.             RegCloseKey(key2);
  47.         }
  48.         RegCloseKey(key1);
  49.     }
  50. }
  51.  
  52. void FlyWriteProfile(LPTSTR section, LPCTSTR key, DWORD dwType, CONST BYTE *lpData, DWORD cbData)
  53. {
  54.     HKEY key1,key2,key3,key4;
  55.     DWORD d;
  56.  
  57.     if (ERROR_SUCCESS==RegOpenKeyEx(HKEY_CURRENT_USER,"Software",0,KEY_READ,&key1))
  58.     {
  59.         if (ERROR_SUCCESS==RegCreateKeyEx(key1,"Paralelo",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&key2,&d))
  60.         {
  61.             if (ERROR_SUCCESS==RegCreateKeyEx(key2,"Fly3D",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&key3,&d))
  62.             {
  63.                 if (ERROR_SUCCESS==RegCreateKeyEx(key3, section,0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&key4,&d))
  64.                 {
  65.                     RegSetValueEx(key4, key, 0, dwType, lpData, cbData);
  66.                     RegCloseKey(key4);
  67.                 }
  68.                 RegCloseKey(key3);
  69.             }
  70.             RegCloseKey(key2);
  71.         }
  72.         RegCloseKey(key1);
  73.     }
  74. }
  75.  
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CAboutDlg dialog used for App About
  78.  
  79. class CAboutDlg : public CDialog
  80. {
  81. public:
  82.     CAboutDlg();
  83.  
  84. // Dialog Data
  85.     //{{AFX_DATA(CAboutDlg)
  86.     enum { IDD = IDD_ABOUTBOX };
  87.     //}}AFX_DATA
  88.  
  89.     // ClassWizard generated virtual function overrides
  90.     //{{AFX_VIRTUAL(CAboutDlg)
  91.     protected:
  92.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  93.     //}}AFX_VIRTUAL
  94.  
  95. // Implementation
  96. protected:
  97.     //{{AFX_MSG(CAboutDlg)
  98.     //}}AFX_MSG
  99.     DECLARE_MESSAGE_MAP()
  100. };
  101.  
  102. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  103. {
  104.     //{{AFX_DATA_INIT(CAboutDlg)
  105.     //}}AFX_DATA_INIT
  106. }
  107.  
  108. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  109. {
  110.     CDialog::DoDataExchange(pDX);
  111.     //{{AFX_DATA_MAP(CAboutDlg)
  112.     //}}AFX_DATA_MAP
  113. }
  114.  
  115. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  116.     //{{AFX_MSG_MAP(CAboutDlg)
  117.         // No message handlers
  118.     //}}AFX_MSG_MAP
  119. END_MESSAGE_MAP()
  120.  
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CFlyConfigDlg dialog
  123.  
  124. CFlyConfigDlg::CFlyConfigDlg(CWnd* pParent /*=NULL*/)
  125.     : CDialog(CFlyConfigDlg::IDD, pParent)
  126. {
  127.     //{{AFX_DATA_INIT(CFlyConfigDlg)
  128.         // NOTE: the ClassWizard will add member initialization here
  129.     //}}AFX_DATA_INIT
  130.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  131. }
  132.  
  133. void CFlyConfigDlg::DoDataExchange(CDataExchange* pDX)
  134. {
  135.     CDialog::DoDataExchange(pDX);
  136.     //{{AFX_DATA_MAP(CFlyConfigDlg)
  137.         // NOTE: the ClassWizard will add DDX and DDV calls here
  138.     //}}AFX_DATA_MAP
  139. }
  140.  
  141. BEGIN_MESSAGE_MAP(CFlyConfigDlg, CDialog)
  142.     //{{AFX_MSG_MAP(CFlyConfigDlg)
  143.     ON_WM_SYSCOMMAND()
  144.     ON_WM_PAINT()
  145.     ON_WM_QUERYDRAGICON()
  146.     ON_WM_DESTROY()
  147.     ON_BN_CLICKED(IDC_TEST, OnTest)
  148.     ON_BN_CLICKED(IDC_CLOSE, OnClose)
  149.     ON_EN_KILLFOCUS(IDC_BRIGHTNESS, OnKillfocusBrightness)
  150.     //}}AFX_MSG_MAP
  151. END_MESSAGE_MAP()
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // CFlyConfigDlg message handlers
  155.  
  156. BOOL CFlyConfigDlg::OnInitDialog()
  157. {
  158.     CDialog::OnInitDialog();
  159.  
  160.     // Add "About..." menu item to system menu.
  161.  
  162.     // IDM_ABOUTBOX must be in the system command range.
  163.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  164.     ASSERT(IDM_ABOUTBOX < 0xF000);
  165.  
  166.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  167.     if (pSysMenu != NULL)
  168.     {
  169.         CString strAboutMenu;
  170.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  171.         if (!strAboutMenu.IsEmpty())
  172.         {
  173.             pSysMenu->AppendMenu(MF_SEPARATOR);
  174.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  175.         }
  176.     }
  177.  
  178.     SetIcon(m_hIcon, TRUE);            // Set big icon
  179.     SetIcon(m_hIcon, FALSE);        // Set small icon
  180.     
  181.     FlyGetProfile("Settings","videomode", (unsigned char *)&selvideomode, sizeof(int));
  182.     FlyGetProfile("Settings","rendermode", (unsigned char *)&selrendermode, sizeof(int));
  183.     FlyGetProfile("Settings","brightness", (unsigned char *)&brightness, sizeof(float));
  184.     init();
  185.     
  186.     return TRUE;  // return TRUE  unless you set the focus to a control
  187. }
  188.  
  189. void CFlyConfigDlg::OnSysCommand(UINT nID, LPARAM lParam)
  190. {
  191.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  192.     {
  193.         CAboutDlg dlgAbout;
  194.         dlgAbout.DoModal();
  195.     }
  196.     else
  197.     {
  198.         CDialog::OnSysCommand(nID, lParam);
  199.     }
  200. }
  201.  
  202. // If you add a minimize button to your dialog, you will need the code below
  203. //  to draw the icon.  For MFC applications using the document/view model,
  204. //  this is automatically done for you by the framework.
  205.  
  206. void CFlyConfigDlg::OnPaint() 
  207. {
  208.     if (IsIconic())
  209.     {
  210.         CPaintDC dc(this); // device context for painting
  211.  
  212.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  213.  
  214.         // Center icon in client rectangle
  215.         int cxIcon = GetSystemMetrics(SM_CXICON);
  216.         int cyIcon = GetSystemMetrics(SM_CYICON);
  217.         CRect rect;
  218.         GetClientRect(&rect);
  219.         int x = (rect.Width() - cxIcon + 1) / 2;
  220.         int y = (rect.Height() - cyIcon + 1) / 2;
  221.  
  222.         // Draw the icon
  223.         dc.DrawIcon(x, y, m_hIcon);
  224.     }
  225.     else
  226.     {
  227.         CDialog::OnPaint();
  228.     }
  229. }
  230.  
  231. HCURSOR CFlyConfigDlg::OnQueryDragIcon()
  232. {
  233.     return (HCURSOR) m_hIcon;
  234. }
  235.  
  236. int CFlyConfigDlg::init_gl()
  237. {
  238.     int pfd=SendDlgItemMessage(IDC_RENDERMODES,CB_GETITEMDATA,selrendermode,0);
  239.     if (SetPixelFormat(cdc->GetSafeHdc(), pfd, &rendermodes[selrendermode]) == FALSE) 
  240.     {
  241.         MessageBox("Could not set pixel format!","Fly3D Config",MB_OK|MB_ICONEXCLAMATION);
  242.         return 0;
  243.     }
  244.  
  245.     hglrc = wglCreateContext( cdc->GetSafeHdc() );
  246.     if (hglrc==0)
  247.     {
  248.         MessageBox("Could not create rendring context!","Fly3D Config",MB_OK|MB_ICONEXCLAMATION);
  249.         return 0;
  250.     }
  251.  
  252.     wglMakeCurrent( cdc->GetSafeHdc(), hglrc );
  253.  
  254.     char *p;
  255.     p=(char *)glGetString(GL_RENDERER);
  256.     SetDlgItemText(IDC_RENDER,p);
  257.     p=(char *)glGetString(GL_VENDOR);
  258.     SetDlgItemText(IDC_VENDOR,p);
  259.     p=(char *)glGetString(GL_VERSION);
  260.     SetDlgItemText(IDC_VERSION,p);
  261.     p=(char *)glGetString(GL_EXTENSIONS);
  262.     if (p==0||p[0]==0)
  263.         return 1;
  264.  
  265.     SendDlgItemMessage(IDC_EXTENSIONS,LB_RESETCONTENT,0,0);
  266.     char buf[4096];
  267.     strcpy(buf,p);
  268.     char *c1=buf,*c2;
  269.     do{
  270.         c2=strchr(c1,' ');
  271.         if (c2)
  272.             *c2=0;
  273.         SendDlgItemMessage(IDC_EXTENSIONS,LB_ADDSTRING,0,(DWORD)c1);
  274.         if (c2==0)
  275.             break;
  276.         c1=c2+1;
  277.     } while(*c1!=0);
  278.  
  279.     char str[256];
  280.     
  281.     int i=1,j;
  282.     if (p!=0&&strstr(p,"GL_ARB_multitexture"))
  283.         glGetIntegerv(0x84E2,&i);
  284.     sprintf(str,"NTexUnits: %i   ",i);
  285.     strcpy(buf,str);
  286.  
  287.     glGetIntegerv(GL_MAX_LIGHTS,&i);
  288.     sprintf(str,"NLights: %i\r\n",i);
  289.     strcat(buf,str);
  290.  
  291.     glGetIntegerv(GL_DEPTH_BITS,&i);
  292.     sprintf(str,"Depth: %i   ",i);
  293.     strcat(buf,str);
  294.  
  295.     glGetIntegerv(GL_STENCIL_BITS,&i);
  296.     sprintf(str,"Stencil: %i   ",i);
  297.     strcat(buf,str);
  298.  
  299.     glGetIntegerv(GL_ACCUM_ALPHA_BITS,&i);
  300.     j=i;
  301.     glGetIntegerv(GL_ACCUM_RED_BITS,&i);
  302.     j+=i;
  303.     glGetIntegerv(GL_ACCUM_BLUE_BITS,&i);
  304.     j+=i;
  305.     glGetIntegerv(GL_ACCUM_GREEN_BITS,&i);
  306.     j+=i;
  307.     sprintf(str,"Accum: %i   ",j);
  308.     strcat(buf,str);
  309.  
  310.     glGetIntegerv(GL_ALPHA_BITS,&i);
  311.     sprintf(str,"Alpha: %i",i);
  312.     strcat(buf,str);
  313.     
  314.     SetDlgItemText(IDC_INFO,buf);
  315.  
  316.     return 1;
  317. }
  318.  
  319. void CFlyConfigDlg::free_gl()
  320. {
  321.     if (hglrc==0)
  322.         return;
  323.  
  324.     wglMakeCurrent(NULL, NULL);
  325.     wglDeleteContext(hglrc);
  326.     hglrc=0;
  327. }
  328.  
  329. void CFlyConfigDlg::free()
  330. {
  331.     ReleaseDC(cdc);
  332.     cdc=0;
  333.     numvideomodes=0;
  334.     numrendermodes=0;
  335.     selrendermode=0;
  336. }
  337.  
  338. void CFlyConfigDlg::init()
  339. {
  340.     char str[256];
  341.     int i,j,k,numaccel;
  342.  
  343.     cdc=GetDC();
  344.     colorbits=cdc->GetDeviceCaps(BITSPIXEL);
  345.     sprintf(str,"%i bits/pixel",colorbits);
  346.     SetDlgItemText(IDC_COLORDEPTH,str);
  347.     sprintf(str,"%.2f",brightness);
  348.     SetDlgItemText(IDC_BRIGHTNESS,str);
  349.     
  350.     SendDlgItemMessage(IDC_VIDEOMODES,CB_RESETCONTENT,0,0);
  351.     SendDlgItemMessage(IDC_RENDERMODES,CB_RESETCONTENT,0,0);
  352.     SetDlgItemText(IDC_RENDER,"");
  353.     SetDlgItemText(IDC_VENDOR,"");
  354.     SetDlgItemText(IDC_VERSION,"");
  355.     GetDlgItem(IDOK)->EnableWindow(0);
  356.     GetDlgItem(IDC_CLOSE)->EnableWindow(0);
  357.  
  358.     i=0;
  359.     while (EnumDisplaySettings(NULL,i,&videomodes[numvideomodes]))
  360.     {
  361.         if ((int)videomodes[numvideomodes].dmBitsPerPel==colorbits)
  362.         {
  363.             if (videomodes[numvideomodes].dmDisplayFrequency==0)
  364.                 sprintf(str,"Resolution: %ix%i",
  365.                     videomodes[numvideomodes].dmPelsWidth,videomodes[numvideomodes].dmPelsHeight);
  366.             else
  367.                 sprintf(str,"Resolution: %ix%i   Refresh: %i ",
  368.                     videomodes[numvideomodes].dmPelsWidth,videomodes[numvideomodes].dmPelsHeight,videomodes[numvideomodes].dmDisplayFrequency);
  369.             SendDlgItemMessage(IDC_VIDEOMODES,CB_ADDSTRING,0,(LPARAM)str);
  370.             numvideomodes++;
  371.         }
  372.         i++;
  373.     }
  374.  
  375.     numaccel=0;
  376.     i=1;
  377.     j=DescribePixelFormat(cdc->GetSafeHdc(),i,sizeof(PIXELFORMATDESCRIPTOR),&rendermodes[i]);
  378.     numrendermodes=0;
  379.     while(i<j)
  380.     {
  381.         DescribePixelFormat(cdc->GetSafeHdc(),i,sizeof(PIXELFORMATDESCRIPTOR),&rendermodes[numrendermodes]);
  382.         if (rendermodes[numrendermodes].iPixelType==0 && 
  383.             (rendermodes[numrendermodes].dwFlags&PFD_DOUBLEBUFFER) &&
  384.             rendermodes[numrendermodes].cColorBits==colorbits)
  385.             {
  386.             sprintf(str,"%s  Depth:%i  Stencil:%i  Accum:%i  Alpha:%i" ,
  387.                 (rendermodes[numrendermodes].dwFlags&PFD_GENERIC_FORMAT)?"Genetric":"Accelerated",
  388.                 rendermodes[numrendermodes].cDepthBits,
  389.                 rendermodes[numrendermodes].cStencilBits,
  390.                 rendermodes[numrendermodes].cAccumBits,
  391.                 rendermodes[numrendermodes].cAlphaBits);
  392.             if ((rendermodes[numrendermodes].dwFlags&PFD_GENERIC_FORMAT)==0)
  393.                 numaccel++;
  394.             k=SendDlgItemMessage(IDC_RENDERMODES,CB_ADDSTRING,0,(LPARAM)str);
  395.             SendDlgItemMessage(IDC_RENDERMODES,CB_SETITEMDATA,k,i);
  396.             numrendermodes++;
  397.             }
  398.         i++;
  399.     }
  400.  
  401.     if (colorbits<16)
  402.     {
  403.         GetDlgItem(IDC_TEST)->EnableWindow(0);
  404.         MessageBox("Current color depth is not supported!\nMust be in 16, 24 or 32 bits/pixel video mode.","Fly3D Config",MB_ICONEXCLAMATION);
  405.     }
  406.     else GetDlgItem(IDC_TEST)->EnableWindow(1);
  407.     
  408.     if (selvideomode<numvideomodes)
  409.         SendDlgItemMessage(IDC_VIDEOMODES,CB_SETCURSEL,selvideomode,0);
  410.     if (selrendermode<numrendermodes)
  411.         SendDlgItemMessage(IDC_RENDERMODES,CB_SETCURSEL,selrendermode,0);
  412. }
  413.  
  414. void CFlyConfigDlg::OnDestroy() 
  415. {
  416.     CDialog::OnDestroy();
  417.  
  418.     free_gl();
  419.     free();
  420. }
  421.  
  422.  
  423. void CFlyConfigDlg::OnOK() 
  424. {
  425.     selvideomode=SendDlgItemMessage(IDC_VIDEOMODES,CB_GETCURSEL,1,0);
  426.     selrendermode=SendDlgItemMessage(IDC_RENDERMODES,CB_GETCURSEL,1,0);
  427.  
  428.     char str[256];
  429.     GetDlgItemText(IDC_BRIGHTNESS,str,255);
  430.     sscanf(str,"%f",&brightness);
  431.  
  432.     if (rendermodes[selrendermode].dwFlags&PFD_GENERIC_FORMAT)
  433.         MessageBox("A generic (not accelerated) render mode has been selected.\nRendering in software will make game play very slow.","Fly3D Config",MB_ICONINFORMATION);
  434.  
  435.     FlyWriteProfile("Settings","videomode", REG_DWORD, (unsigned char *)&selvideomode, sizeof(int));    
  436.     FlyWriteProfile("Settings","rendermode", REG_DWORD, (unsigned char *)&selrendermode, sizeof(int));    
  437.     FlyWriteProfile("Settings","brightness", REG_DWORD, (unsigned char *)&brightness, sizeof(float));    
  438.     FlyWriteProfile("Settings","colorbits", REG_DWORD, (unsigned char *)&colorbits, sizeof(int));    
  439.  
  440.     EndDialog(IDOK);
  441. }
  442.  
  443. void CFlyConfigDlg::OnTest() 
  444. {
  445.     selvideomode=SendDlgItemMessage(IDC_VIDEOMODES,CB_GETCURSEL,1,0);
  446.     selrendermode=SendDlgItemMessage(IDC_RENDERMODES,CB_GETCURSEL,1,0);
  447.     GetDlgItem(IDC_VIDEOMODES)->EnableWindow(0);
  448.     GetDlgItem(IDC_RENDERMODES)->EnableWindow(0);
  449.     GetDlgItem(IDC_COLORDEPTH)->EnableWindow(0);
  450.     GetDlgItem(IDC_BRIGHTNESS)->EnableWindow(0);
  451.     GetDlgItem(IDC_TEST)->EnableWindow(0);
  452.     GetDlgItem(IDOK)->EnableWindow(1);
  453.     GetDlgItem(IDC_CLOSE)->EnableWindow(1);
  454.     
  455.     free_gl();
  456.     init_gl();    
  457. }
  458.  
  459. LRESULT CFlyConfigDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  460. {
  461.     if (message==WM_DISPLAYCHANGE)    
  462.     {
  463.         EndDialog(IDOK);
  464.     }
  465.     return CDialog::WindowProc(message, wParam, lParam);
  466. }
  467.  
  468. void CFlyConfigDlg::OnCancel() 
  469. {
  470.     EndDialog(IDCANCEL);
  471. }
  472.  
  473. void CFlyConfigDlg::OnClose() 
  474. {
  475.     EndDialog(IDOK);
  476. }
  477.  
  478.  
  479. void CFlyConfigDlg::OnKillfocusBrightness() 
  480. {
  481.     char str[256];
  482.     float f=0;
  483.     GetDlgItemText(IDC_BRIGHTNESS,str,255);
  484.     sscanf(str,"%f",&f);
  485.     sprintf(str,"%.2f",f);
  486.     SetDlgItemText(IDC_BRIGHTNESS,str);
  487. }
  488.